//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.point.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.point.entity.PointSyncEntity;
import ciicos.point.model.pointsync.PointSyncForm;
import ciicos.point.model.pointsync.PointSyncInfoVO;
import ciicos.point.model.pointsync.PointSyncListVO;
import ciicos.point.model.pointsync.PointSyncPagination;
import ciicos.point.service.PointSyncService;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"点位同步表"},
    value = "pointManage"
)
@RequestMapping({"/api/pointManage/PointSync"})
public class PointSyncController {
    private static final Logger log = LoggerFactory.getLogger(PointSyncController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private PointSyncService pointSyncService;

    @ApiOperation("获取列表")
    @PostMapping({"/getAll"})
    public ActionResult getAll() {
        List<PointSyncEntity> list = this.pointSyncService.list();
        return ActionResult.success(list);
    }

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody PointSyncPagination pointSyncPagination) throws IOException {
        List<PointSyncEntity> list = this.pointSyncService.getList(pointSyncPagination);
        List<PointSyncListVO> listVO = JsonUtil.getJsonToList(list, PointSyncListVO.class);

        for(PointSyncListVO pointSyncVO : listVO) {
            pointSyncVO.setNode_name(pointSyncVO.getNode());
            pointSyncVO.setCode_name(pointSyncVO.getCode());
            pointSyncVO.setName_name(pointSyncVO.getName());
            pointSyncVO.setType_name(this.generaterSwapUtil.getDicName(pointSyncVO.getType(), "436925156615521477"));
            pointSyncVO.setUnit_name(pointSyncVO.getUnit_name());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(pointSyncPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid PointSyncForm pointSyncForm) throws DataException {
        String b = this.pointSyncService.checkForm(pointSyncForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            PointSyncEntity entity = (PointSyncEntity)JsonUtil.getJsonToBean(pointSyncForm, PointSyncEntity.class);
            entity.setId(mainId);
            this.pointSyncService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<PointSyncInfoVO> info(@PathVariable("id") String id) {
        PointSyncEntity entity = this.pointSyncService.getInfo(id);
        PointSyncInfoVO vo = (PointSyncInfoVO)JsonUtil.getJsonToBean(entity, PointSyncInfoVO.class);
        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<PointSyncInfoVO> detailInfo(@PathVariable("id") String id) {
        PointSyncEntity entity = this.pointSyncService.getInfo(id);
        PointSyncInfoVO vo = (PointSyncInfoVO)JsonUtil.getJsonToBean(entity, PointSyncInfoVO.class);
        vo.setNode(vo.getNode());
        vo.setCode(vo.getCode());
        vo.setName(vo.getName());
        vo.setType(this.generaterSwapUtil.getDicName(vo.getType(), "436925156615521477"));
        return ActionResult.success(vo);
    }

    @ApiOperation("同步数据")
    @GetMapping({"/syncAll"})
    @Transactional
    public ActionResult<PointSyncInfoVO> syncAll() {
        this.pointSyncService.syncAll();
        return ActionResult.success("同步已经完成");
    }
}
